import { defineStore } from 'pinia'
import { ref } from "vue"
import { SessionStorage } from 'quasar'



export const useUserStore = defineStore('user', () => {

  const token = ref(SessionStorage.getItem("token") || "");
  const uid = ref(SessionStorage.getItem("uid") || null);
  const name = ref(SessionStorage.getItem("name") || "");
  const headimgurl = ref(SessionStorage.getItem("headimgurl") || "");

  function setToken(token_value) {
    token.value = token_value
    SessionStorage.set("token", token_value)
  }

  function getToken() {
    return token.value
  }

  function setUID(uid_value) {
    uid.value = uid_value
    SessionStorage.set("uid", uid_value)
  }

  function getUID() {
    return uid.value
  }

  function setName(name_value) {
    name.value = name_value
    SessionStorage.set("name", name_value)
  }

  function getName() {
    return name.value
  }

  function setHeadimgurl(img_value) {
    headimgurl.value = img_value
    SessionStorage.set("headimgurl", img_value)
  }

  function getHeadimgurl() {
    return headimgurl.value
  }

  function setUserInfo(data) {
    token.value = data.token
    SessionStorage.set("token", data.token)

    uid.value = data.uid
    SessionStorage.set("uid", data.uid)

    name.value = data.name
    SessionStorage.set("name", data.name)

    headimgurl.value = data.headimgurl
    SessionStorage.set("headimgurl", data.headimgurl)
  }

  function clearAll() {
    token.value = "";
    uid.value = null;
    name.value = "";
    headimgurl.value = "";
    SessionStorage.remove("uid");
    SessionStorage.remove("name");
    SessionStorage.remove("token");
    SessionStorage.remove("headimgurl");
  }

  return {
    token,
    uid,
    name,
    headimgurl,
    setToken,
    clearAll,
    getToken,
    setUID,
    getUID,
    setName,
    getName,
    setHeadimgurl,
    getHeadimgurl,
    setUserInfo,
  }

})
